<template>
  <div class="app-container">
    <el-form ref="ruleForm" v-loading="loading" :model="ruleForm" :rules="rules" label-width="100px">

      <el-form-item label="业主" prop="proprietor">
        <el-select v-model="ruleForm.proprietor" placeholder="请选择业主" @change="selectGet">
          <el-option
              v-for="item in accountList"
              :key="item.accountId"
              :label="item.userName"
              :value="item.accountId"
          >
            <span style="float: left"  id="username">{{ item.userName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px"  id="accountId">{{ item.accountId }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="总空间 ㎡" prop="area">
        <el-input-number v-model="ruleForm.area" :precision="2" :step="0.1" :min="0" />
      </el-form-item>
      <el-form-item label="居住空间 ㎡" prop="real_area">
        <el-input-number v-model="ruleForm.real_area" :precision="2" :step="0.1" :min="0" />
      </el-form-item>
      <el-form-item label="房源地址" prop="address">
        <el-input v-model="ruleForm.address" :precision="2" :step="0.1" :min="0" />
      </el-form-item>
      <el-form-item label="房屋朝向" prop="as_aga">
        <el-input v-model="ruleForm.as_aga" :precision="2" :step="0.1" :min="0" />
      </el-form-item>
      <el-form-item label="房屋类型" prop="house_type">
        <el-input v-model="ruleForm.house_type" :precision="2" :step="0.1" :min="0" />
      </el-form-item>
      <el-form-item label="房屋楼层" prop="floor">
        <el-input-number
            v-model="ruleForm.floor"
            :controls="false"
            :step="1"
            :min="0"
            :max="100"
        />
      </el-form-item>
      <el-form-item label="房屋价格" prop="house_price">
        <el-input v-model="ruleForm.house_price" :precision="2" :step="0.1" :min="0" @blur="formatPrice" />
      </el-form-item>
      <el-form-item label="房屋图片" prop="house_images">
        <el-upload
            action="/api/v1/file"
            list-type="picture"
            name="file">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="是否装修" prop="is_fitment">
        <el-input-number
            v-model="ruleForm.is_fitment"
            :controls="false"
            :step="1"
            :min="0"
            :max="1"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { queryAccountList } from '@/api/account'
import { createRealEstate } from '@/api/realEstate'

export default {
  name: 'AddRealeState',
  data() {
    var checkArea = (rule, value, callback) => {
      if (value <= 0) {
        callback(new Error('必须大于0'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {

        proprietor: '',
        area: 0,
        real_area: 0,
        address: '',
        as_aga: '',
        house_type: '',
        floor: 0,
        house_price: 0.0,
        is_fitment: 0,
        house_images:"",
        file:'',
      },
      accountList: [],
      rules: {
        proprietor: [
          { required: true, message: '请选择业主', trigger: 'change' }
        ],
        area: [
          { validator: checkArea, trigger: 'blur' }
        ],
        real_area: [
          { validator: checkArea, trigger: 'blur' }
        ],
        address: [
          { required: true,message: '请输入房源地址', trigger: 'blur' }
        ],
        as_aga: [
          { required: true,message: '请输入房屋朝向', trigger: 'blur' }
        ],
        house_type: [
          { required: true,message: '请输入房屋类型', trigger: 'blur' }
        ],
        floor: [
          { required: true,message: '请输入房屋楼层', trigger: 'blur' }
        ],
        house_price: [
          { required: true,message: '请输入房屋价格', trigger: 'blur' }
        ],
        is_fitment: [
          { required: true,message: '房屋是否装修', trigger: 'blur' }
        ],
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters([
      'accountId'
    ])
  },
  created() {
    queryAccountList().then(response => {
      if (response !== null) {
        // 过滤掉管理员
        this.accountList = response.filter(item =>
            item.userName !== '管理员'
        )
      }
    })
  },
  methods: {

    submitForm(formName) {
// 直接使用响应式数据
      let username = this.selectedUserName; // 假设你已经在 selectGet 方法中设置了这个值
      let accountId = this.ruleForm.proprietor;
      console.log(username, accountId);

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否立即创建?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'success'
          }).then(() => {
            this.loading = true
            createRealEstate({
              username: username,
              accountId: accountId,
              proprietor: this.ruleForm.proprietor,
              area: this.ruleForm.area,
              real_area: this.ruleForm.real_area,
              address: this.ruleForm.address,
              as_aga: this.ruleForm.as_aga,
              house_type: this.ruleForm.house_type,
              floor: this.ruleForm.floor,
              house_price: this.ruleForm.house_price,
              is_fitment: this.ruleForm.is_fitment,

            }).then(response => {
              console.log(response)
              this.loading = false
              if (response !== null) {
                this.$message({
                  type: 'success',
                  message: '创建成功!'
                })
              } else {
                this.$message({
                  type: 'error',
                  message: '创建失败!'
                })
              }
            }).catch(_ => {
              this.loading = false
            })
          }).catch(() => {
            this.loading = false
            this.$message({
              type: 'info',
              message: '已取消创建'
            })
          })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    selectGet(accountId) {

      // 根据 accountId 找到对应的用户信息
      const selectedAccount = this.accountList.find(account => account.accountId === accountId);
      if (selectedAccount) {
        this.selectedUserName = selectedAccount.userName; // 存储选中的 userName
      }
    },
    formatPrice() {
      if (this.ruleForm.house_price !== null) {
        this.ruleForm.house_price = parseFloat(this.ruleForm.house_price);
      }
    }
  }
}
</script>

<style scoped>
</style>
